Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address
Withoutbook LIVE Mock Interviews

Freshers / Beginner level questions & answers

Ques 1. What is Object-Oriented Programming (OOP)?

OOP is a programming paradigm that uses objects to organize code. It involves concepts like encapsulation, inheritance, and polymorphism.

Is it helpful? Add Comment View Comments
 

Ques 2. Explain the concept of encapsulation in Java.

Encapsulation is the bundling of data and methods that operate on the data into a single unit known as a class. It helps in hiding the implementation details and restricting access to the internal state of an object.

Example:

class BankAccount {
  private double balance;
  // other methods and fields
}

Is it helpful? Add Comment View Comments
 

Ques 3. What is the difference between abstraction and encapsulation?

Abstraction involves hiding the complex reality while encapsulation involves bundling the data and the methods that operate on the data into a single unit.

Is it helpful? Add Comment View Comments
 

Ques 4. How does inheritance work in Java?

Inheritance allows a class to inherit properties and behaviors from another class. It promotes code reusability and establishes a relationship between a superclass and a subclass.

Example:

class Animal {
 /* properties and methods */
 }
class Dog extends Animal { /* additional methods and properties */ }

Is it helpful? Add Comment View Comments
 

Ques 5. Explain polymorphism in Java.

Polymorphism allows objects of different types to be treated as objects of a common type. It can be achieved through method overloading and method overriding.

Example:

void drawShape(Shape s) {
 /* method implementation */
 }

Is it helpful? Add Comment View Comments
 

Ques 6. What is the 'super' keyword in Java?

The 'super' keyword is used to refer to the superclass, invoking its methods, or accessing its fields. It is often used in the context of method overriding.

Example:

super.display();

Is it helpful? Add Comment View Comments
 

Ques 7. What is the purpose of the 'final' keyword?

The 'final' keyword is used to make a variable, method, or class constant and cannot be changed. It also prevents a class from being subclassed.

Example:

final int MAX_VALUE = 100;

Is it helpful? Add Comment View Comments
 

Ques 8. What is an abstract class in Java?

An abstract class is a class that cannot be instantiated on its own and may contain abstract methods. It is meant to be subclassed by concrete classes.

Example:

abstract class Shape { /* abstract methods */ }

Is it helpful? Add Comment View Comments
 

Ques 9. Explain the difference between an interface and an abstract class.

An interface in Java is a collection of abstract methods, while an abstract class can have both abstract and concrete methods. A class can implement multiple interfaces but can extend only one abstract class.

Is it helpful? Add Comment View Comments
 

Ques 10. What is the purpose of the 'this' keyword?

The 'this' keyword is used to refer to the current instance of the class. It is often used to differentiate instance variables from local variables when they have the same name.

Example:

public void setName(String name) { this.name = name; }

Is it helpful? Add Comment View Comments
 

Ques 11. What is the purpose of the 'static' keyword in Java?

The 'static' keyword is used to create class-level variables and methods. These belong to the class rather than instances of the class.

Example:

static int count = 0;
static void increment() { count++; }

Is it helpful? Add Comment View Comments
 

Ques 12. Explain the concept of composition in Java.

Composition involves creating complex objects by combining simpler objects. It allows for better code organization and reusability.

Example:

class Car {
  Engine engine;
  // other properties and methods
}

Is it helpful? Add Comment View Comments
 

Ques 13. What is the purpose of the 'interface' in Java?

An interface in Java is a collection of abstract methods that defines a contract for classes to implement. It supports multiple inheritances and is often used for achieving abstraction.

Example:

interface Printable { void print(); }

Is it helpful? Add Comment View Comments
 

Ques 14. What is the purpose of the 'final' method in Java?

A 'final' method in Java cannot be overridden by subclasses. It is used to prevent modification of a method in a subclass.

Example:

class Parent {
  final void display() { /* method implementation */ }
}

Is it helpful? Add Comment View Comments
 

Ques 15. Explain the concept of method overloading in Java.

Method overloading allows a class to have multiple methods with the same name but different parameters. It improves code readability and flexibility.

Example:

int add(int a, int b) {
 /* method implementation */
 }
double add(double a, double b) { /* method implementation */ }

Is it helpful? Add Comment View Comments
 

Ques 16. What is the 'instanceof' operator used for?

The 'instanceof' operator in Java is used to test if an object is an instance of a particular class or interface. It returns true if the object is an instance; otherwise, it returns false.

Example:

if (obj instanceof MyClass) { /* do something */ }

Is it helpful? Add Comment View Comments
 

Ques 17. Explain the concept of method overriding in Java.

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. It allows for runtime polymorphism.

Example:

class Animal { void makeSound() { /* method implementation */ } }
class Dog extends Animal { void makeSound() { /* overridden method implementation */ } }

Is it helpful? Add Comment View Comments
 

Ques 18. What is a constructor in Java?

A constructor in Java is a special method used to initialize objects. It has the same name as the class and is called when an object is created.

Example:

class Car { Car() { /* constructor code */ } }

Is it helpful? Add Comment View Comments
 

Ques 19. Explain the 'super()' statement in a constructor.

The 'super()' statement is used to invoke the constructor of the superclass. It must be the first statement in the subclass constructor if used.

Example:

class SubClass extends SuperClass { SubClass() { super(); /* other constructor code */ } }

Is it helpful? Add Comment View Comments
 

Ques 20. What is the 'this()' constructor call in Java?

The 'this()' constructor call is used to invoke another constructor in the same class. It must be the first statement in the constructor if used.

Example:

class MyClass { MyClass(int x) { this(); /* other constructor code */ } 
MyClass() { /* default constructor code */ } }

Is it helpful? Add Comment View Comments
 

Ques 21. What is a singleton class in Java?

A singleton class in Java is a class that allows only one instance to be created. It provides a global point of access to that instance.

Example:

class Singleton { private static Singleton instance = new Singleton(); 
private Singleton() {} 
public static Singleton getInstance() { return instance; } }

Is it helpful? Add Comment View Comments
 

Ques 22. Explain the concept of abstract method in Java.

An abstract method is a method declared without an implementation in an abstract class. Subclasses must provide the implementation for abstract methods.

Example:

abstract class Shape { abstract void draw(); }

Is it helpful? Add Comment View Comments
 

Ques 23. What is the 'throws' clause in Java?

The 'throws' clause in Java is used to declare exceptions that a method might throw. It indicates that the method does not handle the exceptions and passes them to its caller.

Example:

void myMethod() throws IOException { /* method code */ }

Is it helpful? Add Comment View Comments
 

Ques 24. Explain the 'try', 'catch', and 'finally' blocks in Java.

The 'try' block contains the code that might throw an exception. The 'catch' block handles the exception, and the 'finally' block contains code that is always executed, regardless of whether an exception is thrown or not.

Example:

try { /* code that might throw an exception */ } 
catch (Exception e) { /* handle the exception */ } 
finally { /* code that always executes */ }

Is it helpful? Add Comment View Comments
 

Ques 25. What is method chaining in Java?

Method chaining involves invoking multiple methods on the same object in a single line. It enhances code readability and is often used in builder patterns.

Example:

StringBuilder sb = new StringBuilder().append("Hello").append(",").append(" World");

Is it helpful? Add Comment View Comments
 

Ques 26. Explain the concept of the 'Object' class in Java.

The 'Object' class is the root class for all Java classes. It provides common methods such as 'toString()' and 'equals()' that can be overridden by subclasses.

Example:

class MyClass { /* methods and fields */ }
// implicitly extends Object class

Is it helpful? Add Comment View Comments
 

Ques 27. What is the purpose of the 'clone()' method in Java?

The 'clone()' method is used to create a copy of an object. The class of the object must implement the 'Cloneable' interface to use this method.

Example:

class MyClass implements Cloneable { /* methods and fields */ 
public Object clone() throws CloneNotSupportedException { return super.clone(); } }

Is it helpful? Add Comment View Comments
 

Ques 28. Explain the concept of the 'enum' type in Java.

An 'enum' type in Java is a special data type used to define a fixed set of constants. It is often used for representing a group of related values.

Example:

enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

Is it helpful? Add Comment View Comments
 

Experienced / Expert level questions & answers

Ques 29. Explain the 'diamond problem' and how Java solves it.

The 'diamond problem' occurs in multiple inheritance when a class inherits from two classes that have a common ancestor. In Java, this is avoided by allowing a class to implement multiple interfaces.

Is it helpful? Add Comment View Comments
 

Ques 30. Explain the concept of method hiding in Java.

Method hiding occurs when a subclass provides a static method with the same signature as a static method in its superclass. It does not override the superclass method.

Example:

class Parent { static void display() { /* method implementation */ } }
class Child extends Parent { static void display() { /* hiding method implementation */ } }

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

All interview subjects

©2024 WithoutBook